﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace ProceduralGolf
{
    class TextureSmoother : TextureEffect
    {
        private GraphicsDevice graphics;
        private Effect colorEffect;

        public TextureSmoother(Game game)
        {
            this.graphics = game.GraphicsDevice;
            colorEffect = game.Content.Load<Effect>("smooth");
            spriteBatch = new SpriteBatch(graphics);
        }


        public Texture2D smooth(Texture2D tex, Texture2D smooth, Texture2D mask)
        {
            GraphicsMutex.GetMutex().WaitOne();

            Texture2D result;
            RenderTarget2D target = new RenderTarget2D(graphics, tex.Width, tex.Height, 1, graphics.PresentationParameters.BackBufferFormat);
            DepthStencilBuffer stencilBuffer = new DepthStencilBuffer(graphics, tex.Width, tex.Height, graphics.DepthStencilBuffer.Format);
            DepthStencilBuffer oldBuffer = graphics.DepthStencilBuffer;

            graphics.DepthStencilBuffer = stencilBuffer;
            graphics.SetRenderTarget(0, target);
            graphics.Clear(Color.Black);

            colorEffect.Begin();
            colorEffect.Parameters["SmoothTexture"].SetValue(smooth);
            colorEffect.Parameters["Mask"].SetValue(mask);

            foreach (EffectPass pass in colorEffect.CurrentTechnique.Passes)
            {
                spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None);
                pass.Begin();

                spriteBatch.Draw(tex, new Rectangle(0, 0, tex.Width, tex.Height), Color.White);
                pass.End();
                spriteBatch.End();
            }

            colorEffect.End();

            graphics.SetRenderTarget(0, null);
            graphics.DepthStencilBuffer = oldBuffer;

            result = target.GetTexture();

            stencilBuffer.Dispose();
            target.Dispose();

            GraphicsMutex.GetMutex().ReleaseMutex();

            return result;

        }

        //public Texture2D smooth(Texture2D tex, Texture2D smooth, Texture2D mask)
        //{
        //    Texture2D result;
        //    RenderTarget2D target = new RenderTarget2D(graphics, tex.Width, tex.Height, 1, graphics.PresentationParameters.BackBufferFormat);

        //    graphics.SetRenderTarget(0, target);
            
        //    colorEffect.Begin();
        //    colorEffect.Parameters["SmoothTexture"].SetValue(smooth);
        //    colorEffect.Parameters["Mask"].SetValue(mask);

        //    foreach (EffectPass pass in colorEffect.CurrentTechnique.Passes)
        //    {
        //        spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None);
        //        pass.Begin();

        //        spriteBatch.Draw(tex, new Rectangle(0, 0, tex.Width, tex.Height), Color.White);
        //        pass.End();
        //        spriteBatch.End();
        //    }

        //    colorEffect.End();

        //    graphics.SetRenderTarget(0, null);
        //    result = target.GetTexture();
        //    target.Dispose();

        //    return result;

        //}


    }
}
